# -*- coding: utf-8 -*-

#    Copyright (C) 2014 by Promotux
#                       di Francesco Meloni snc - http://www.promotux.it/

#    Author: Francesco Meloni  <francesco@promotux.it>

#    This file is part of OdCollect.

#    OdCollect is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.

#    OdCollect is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with OdCollect.  If not, see <http://www.gnu.org/licenses/>.

import hashlib
from sqlalchemy import *
from sqlalchemy.orm import *
from Dao import Dao
from Environment import *
from lib.utils import orda
from web_lib.webutils import leggiInSessione

try:
    t_setconf = Table('setconf', meta,
                    schema=leggiInSessione(sezione="Main", chiave="schema"), autoload=True)
except:
    from data.setconf import t_setconf


class SetConf(Dao):

    def __init__(self, req=None, arg=None):
        Dao.__init__(self, entity=self)

    def filter_values(self, k, v):
        if k == "description":
            dic = {k: t_setconf.c.description.ilike("%" + v + "%")}
        elif k == "value":
            dic = {k: t_setconf.c.value == v}
        elif k == "body":
            dic = {k: t_setconf.c.body.ilike("%" + v + "%")}
        elif k == "section":
            dic = {k: t_setconf.c.section == v}
        elif k == "key":
            dic = {k: t_setconf.c.key == v}
        elif k == 'searchkey':
            dic = {k: or_(t_setconf.c.key.ilike("%" + v + "%"),
                        t_setconf.c.value.ilike("%" + v + "%"),
                        t_setconf.c.description.ilike("%" + v + "%"))}
        elif k == "active":
            dic = {k: t_setconf.c.active == v}
        elif k == "visible":
            dic = {k: t_setconf.c.visible == v}
        return  dic[k]

std_mapper = mapper(SetConf, t_setconf,
    properties={
        'description': deferred(t_setconf.c.description),
        'date': deferred(t_setconf.c.date),
    }, order_by=t_setconf.c.key)

allkey = session.query(SetConf.key, SetConf.section).all()
bb = SetConf().select(section="Primanota", batchSize=None)
for b in bb:
    session.delete(b)
    session.commit()
a = False


if not ("valuta_curr", "Valuta") in allkey:
    kbb = SetConf()
    kbb.key = "valuta_curr"
    kbb.value = "€"
    kbb.section = "Valuta"
    kbb.tipo_section = "Generico"
    kbb.description = "Vvaluta"
    kbb.active = True
    kbb.tipo = "str"
    kbb.date = datetime.datetime.now()
    session.add(kbb)
    a = True

if not ("head_title", "Project") in allkey:
    key_mf = SetConf()
    key_mf.key = "head_title"
    key_mf.value = "PromoTUX - Virtual company - Servizi e soluzioni gestionali per la tua azienda - Open source e Linux - Promotux snc"
    key_mf.section = "Project"
    key_mf.description = "Titolo standar della pagina principale"
    key_mf.active = True
    key_mf.date = datetime.datetime.now()
    session.add(key_mf)
    a = True

if not ("news_list_number", "Project") in allkey:
    key_nln = SetConf()
    key_nln.key = "news_list_number"
    key_nln.value = "3"
    key_nln.section = "Project"
    key_nln.description = "Numero di news nella sidebar"
    key_nln.active = True
    key_nln.date = datetime.datetime.now()
    session.add(key_nln)
    a = True

if not ("smtpserver", "Project") in allkey:
    key_nln = SetConf()
    key_nln.key = "smtpserver"
    key_nln.value = "smtp.tiscali.it"
    key_nln.section = "Project"
    key_nln.description = "server smtp con cui spedire le email"
    key_nln.active = True
    key_nln.date = datetime.datetime.now()
    session.add(key_nln)
    a = True

if not ("separatore_numerazione", "Documenti") in allkey:
    kpp = SetConf()
    kpp.key = "separatore_numerazione"
    kpp.value = "/"
    kpp.section = "Documenti"
    kpp.description = "separatore numerazione"
    kpp.tipo_section = "Generico"
    kpp.active = True
    kpp.date = datetime.datetime.now()
    session.add(kpp)
    a = True


if not ("ricerca_per", "Documenti") in allkey:
    krr = SetConf()
    krr.key = "ricerca_per"
    krr.value = "codice"
    krr.section = "Documenti"
    krr.description = "Preimposta un tipo di ricerca Valori possibili:(codice,descrizione,codice_a_barre,codice_articolo_fornitore "
    krr.tipo_section = "Generico"
    krr.active = True
    krr.visible = True
    krr.date = datetime.datetime.now()
    session.add(krr)
    a = True

#if not ("cartella_predefinita", "General") in allkey:
    #krr = SetConf()
    #krr.key = "cartella_predefinita"
    #krr.value = documentsDir
    #krr.section = "General"
    #krr.description = "Cartella di salvataggio predefinita"
    #krr.tipo_section = "Generico"
    #krr.active = True
    #krr.visible = True
    #krr.date = datetime.datetime.now()
    #session.add(krr)
    #a = True

if not ("feed", "Feed") in allkey:
    kuu = SetConf()
    kuu.key = "feed"
    kuu.value = "True"
    kuu.section = "Feed"
    kuu.description = "Notizie nella home"
    kuu.tipo_section = "Generico"
    kuu.active = True
    kuu.tipo = "bool"
    kuu.date = datetime.datetime.now()
    session.add(kuu)
    a = True

if not ("emailmittente", "Smtp") in allkey:
    kzz = SetConf()
    kzz.key = "emailmittente"
    kzz.value = ""
    kzz.section = "Smtp"
    kzz.tipo_section = "Generico"
    kzz.description = "Email del mittente"
    kzz.active = True
    kzz.tipo = "str"
    kzz.date = datetime.datetime.now()
    session.add(kzz)
    a = True

if not ("decimals", "Numbers") in allkey:
    kbb = SetConf()
    kbb.key = "decimals"
    kbb.value = "3"
    kbb.section = "Numbers"
    kbb.tipo_section = "Generico"
    kbb.description = "Gestione dei decimali"
    kbb.active = True
    kbb.tipo = "int"
    kbb.date = datetime.datetime.now()
    session.add(kbb)
    a = True

if not ("batch_size", "Numbers") in allkey:
    kcc = SetConf()
    kcc.key = "batch_size"
    kcc.value = "15"
    kcc.section = "Numbers"
    kcc.tipo_section = "Generico"
    kcc.description = "Gestione dei batchSize"
    kcc.active = True
    kcc.tipo = "int"
    kcc.date = datetime.datetime.now()
    session.add(kcc)
    a = True

if not ("combo_column", "Numbers") in allkey:
    kdd = SetConf()
    kdd.key = "combo_column"
    kdd.value = "3"
    kdd.section = "Numbers"
    kdd.tipo_section = "Generico"
    kdd.description = "Gestione dei combo_column cioè le colonne nelle combobox"
    kdd.active = True
    kdd.tipo = "int"
    kdd.date = datetime.datetime.now()
    session.add(kdd)
    a = True

if not ("avvii", "General") in allkey:
    kbb = SetConf()
    kbb.key = "avvii"
    kbb.value = "1"
    kbb.section = "General"
    kbb.tipo_section = "Generico"
    kbb.description = "conta gli avvii"
    kbb.active = True
    kbb.tipo = "int"
    kbb.date = datetime.datetime.now()
    session.add(kbb)
    a = True

if a:
    session.commit()
